package org.yzh.rbac.service;

import org.apache.commons.lang3.StringUtils;
import org.sample.commons.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yzh.rbac.mapper.PermisMapper;
import org.yzh.rbac.model.entity.PermisDO;
import org.yzh.rbac.model.vo.Permis;

import java.util.*;

@Service
public class PermisService {

    @Autowired
    private PermisMapper permisMapper;

    public List<PermisDO> find(PermisDO entity) {
        List<PermisDO> result = permisMapper.find(entity);
        return result;
    }

    public PermisDO get(int id) {
        PermisDO result = permisMapper.get(id);
        return result;
    }

    public Set<String> findUriByRoleId(int id) {
        List<PermisDO> list = permisMapper.findByRoleId(id);
        Set<String> result = new HashSet<>(list.size());

        for (PermisDO entity : list) {
            String uri = entity.getUri();
            if (StringUtils.isNotBlank(uri))
                result.add(uri);
        }
        return result;
    }

    public List<Permis> findByRoleId(int id) {
        List<PermisDO> list = permisMapper.findByRoleId(id);
        List<Permis> result = new ArrayList<>(list.size());

        for (PermisDO entity : list) {
            result.add(BeanUtils.copyBean(entity, new Permis()));
        }
        return result;
    }

    public Set<Permis> findByRoleId(int[] ids) {
        Set<Permis> result = new TreeSet<>();

        for (int id : ids) {
            result.addAll(findByRoleId(id));
        }
        return result;
    }

    public int add(PermisDO entity) {
        int row = permisMapper.insert(entity);
        return row;
    }

    public int update(PermisDO entity) {
        int row = permisMapper.update(entity);
        return row;
    }

    public int delete(Integer id) {
        int row = permisMapper.delete(id);
        return row;
    }
}